package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.BehospitalMapper;
import com.kkb.mapper.DoctorMapper;
import com.kkb.mapper.HosregisterMapper;
import com.kkb.pojo.*;
import com.kkb.vo.BeHospitalQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BeHospitalService {
    @Resource
    private BehospitalMapper behospitalMapper;
    @Resource
    private HosregisterMapper hosregisterMapper;
    @Resource
    private DoctorMapper doctorMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Behospital> queryByPage(Integer pageNum, Integer pageSize, BeHospitalQueryVO vo) {
        BehospitalExample behospitalExample = new BehospitalExample();
        HosregisterExample hosregisterExample = new HosregisterExample();
        DoctorExample doctorExample = new DoctorExample();
        BehospitalExample.Criteria criteria = behospitalExample.createCriteria();
        DoctorExample.Criteria criteria1 = doctorExample.createCriteria();
        HosregisterExample.Criteria criteria2 = hosregisterExample.createCriteria();
        if (vo != null) {
            if (vo.getId() != null) {
                criteria.andIdEqualTo(vo.getId());
            }
            boolean flag = false;
            if (vo.getDoctorName() != null && !"".equals(vo.getDoctorName().trim())) {
                criteria1.andNameEqualTo(vo.getDoctorName().trim());
                flag = true;
            }
            if (vo.getKeShi() != null && !"".equals(vo.getKeShi().trim())) {
                criteria1.andKeShiEqualTo(vo.getKeShi().trim());
                flag = true;
            }
            if (flag) {
                List<Doctor> doctors = doctorMapper.selectByExample(doctorExample);
                List<Integer> idList = new ArrayList<>();
                for (Doctor doctor : doctors) {
                    idList.add(doctor.getId());
                }
                criteria2.andDoctorIdIn(idList);
            }
            if (vo.getBeginDate() != null) {
                criteria2.andDateGreaterThan(vo.getBeginDate());
                flag = true;
            }
            if (vo.getEndDate() != null) {
                criteria2.andDateLessThan(vo.getEndDate());
                flag = true;
            }
            if (flag) {
                List<Hosregister> hosregisters = hosregisterMapper.selectByExample(hosregisterExample);
                List<Integer> ids = new ArrayList<>();
                for (Hosregister hosregister : hosregisters) {
                    ids.add(hosregister.getId());
                }
                criteria.andHosregisterIdIn(ids);
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Behospital> behospitals = behospitalMapper.selectByExample(behospitalExample);
        for (Behospital behospital : behospitals) {
            behospital.setHosregister(hosregisterMapper.selectByPrimaryKey(behospital.getHosregisterId()));
            behospital.getHosregister().setDoctor(doctorMapper.selectByPrimaryKey(behospital.getHosregister().getDoctorId()));
        }
        return new PageInfo<>(behospitals);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Behospital queryById(Integer id) {
        Behospital behospital = behospitalMapper.selectByPrimaryKey(id);
        behospital.setHosregister(hosregisterMapper.selectByPrimaryKey(behospital.getHosregisterId()));
        behospital.getHosregister().setDoctor(doctorMapper.selectByPrimaryKey(behospital.getHosregister().getDoctorId()));
        return behospital;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteBeHospital(Integer id) {
        Behospital behospital = behospitalMapper.selectByPrimaryKey(id);
        behospital.setState(1);
        behospital.setClosePrice(0);
        return behospitalMapper.updateByPrimaryKeySelective(behospital);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int payBeHospital(Integer id) {
        Behospital behospital = behospitalMapper.selectByPrimaryKey(id);
        behospital.setClosePrice(1);
        return behospitalMapper.updateByPrimaryKeySelective(behospital);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int leaveHospital(Integer id) {
        Behospital behospital = behospitalMapper.selectByPrimaryKey(id);
        behospital.setState(1);
        return behospitalMapper.updateByPrimaryKeySelective(behospital);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteAllBeHospital(ArrayList<Integer> ids) {
        for (int i = 0; i < ids.size(); i++) {
            if (behospitalMapper.selectByPrimaryKey(ids.get(i)).getState() == 1) {
                if (behospitalMapper.selectByPrimaryKey(ids.get(i)).getClosePrice() == 1) {
                    return -1;  //存在已出院的
                } else {
                    return -2; //存在已退院的
                }
            } else {
                if (behospitalMapper.selectByPrimaryKey(ids.get(i)).getClosePrice() == 0) {
                    return -3;  //存在未缴费的
                }
            }
        }
        int count = 0;
        for (int i = 0; i < ids.size(); i++) {
            Behospital behospital = behospitalMapper.selectByPrimaryKey(ids.get(i));
            behospital.setState(1);
            behospital.setClosePrice(0);
            behospitalMapper.updateByPrimaryKeySelective(behospital);
            count++;
        }
        return count;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int leaveAllBeHospital(ArrayList<Integer> ids) {
        for (int i = 0; i < ids.size(); i++) {
            if (behospitalMapper.selectByPrimaryKey(ids.get(i)).getState() == 1) {
                if (behospitalMapper.selectByPrimaryKey(ids.get(i)).getClosePrice() == 1) {
                    return -1;  //存在已出院的
                } else {
                    return -2; //存在已退院的
                }
            } else {
                if (behospitalMapper.selectByPrimaryKey(ids.get(i)).getClosePrice() == 0) {
                    return -3;  //存在未缴费的
                }
            }
        }
        int count = 0;
        for (int i = 0; i < ids.size(); i++) {
            Behospital behospital = behospitalMapper.selectByPrimaryKey(ids.get(i));
            behospital.setState(1);
            behospitalMapper.updateByPrimaryKeySelective(behospital);
            count++;
        }
        return count;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateBeHospital(Behospital behospital) {
        String doctorName = "";
        if ("0".equals(behospital.getDoctorName().trim()))
            doctorName = "程俊";
        else if ("1".equals(behospital.getDoctorName().trim()))
            doctorName = "欧阳雨露";
        else if ("2".equals(behospital.getDoctorName().trim()))
            doctorName = "王博";
        else if ("3".equals(behospital.getDoctorName().trim()))
            doctorName = "沈青川";
        String keShi = "";
        if ("0".equals(behospital.getKeShi().trim()))
            keShi = "急诊科";
        else if ("1".equals(behospital.getKeShi().trim()))
            keShi = "骨科";
        else if ("2".equals(behospital.getKeShi().trim()))
            keShi = "血液科";
        else if ("3".equals(behospital.getKeShi().trim()))
            keShi = "外科";
        DoctorExample doctorExample = new DoctorExample();
        DoctorExample.Criteria criteria = doctorExample.createCriteria();
        criteria.andNameEqualTo(doctorName);
        criteria.andKeShiEqualTo(keShi);
        List<Doctor> doctors = doctorMapper.selectByExample(doctorExample);
        int flag = 0;
        if (doctors.size() == 0 || doctors == null) {
            return -1;
        } else {
            Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(behospitalMapper.selectByPrimaryKey(behospital.getId()).getHosregisterId());
            hosregister.setDoctorId(doctors.get(0).getId());
            hosregister.setSex(behospital.getSex());
            hosregister.setSelfPrice(behospital.getSelfPrice());
            flag = hosregisterMapper.updateByPrimaryKeySelective(hosregister);
        }
        return flag + behospitalMapper.updateByPrimaryKeySelective(behospital);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addBeHospital(Behospital behospital) {
        System.out.println(behospital);
        behospital.setState(0);
        behospital.setClosePrice(0);
        Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(behospital.getHosregisterId());
        hosregister.setState(1);
        hosregisterMapper.updateByPrimaryKeySelective(hosregister);
        return behospitalMapper.insertSelective(behospital);
    }
}
